package com.wmh.baseservice.operationaid.service.impl;


import cn.hutool.core.collection.CollUtil;
import cn.hutool.core.date.LocalDateTimeUtil;
import cn.hutool.core.util.ObjectUtil;
import cn.hutool.core.util.StrUtil;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.wmh.baseservice.common.mybatisplus.structure.LambdaQueryWrapperX;
import com.wmh.baseservice.common.utils.web.UserUtil;
import com.wmh.baseservice.operationaid.mapper.KeyWordMapper;
import com.wmh.baseservice.operationaid.entity.KeyWord;
import com.wmh.baseservice.operationaid.entity.KeyWordUsageRecord;
import com.wmh.baseservice.operationaid.pojo.vo.keyWord.KeyWordTableReqVO;
import com.wmh.baseservice.operationaid.pojo.vo.keyWord.KeyWordTableRespVO;
import com.wmh.baseservice.operationaid.service.KeyWordService;
import com.wmh.baseservice.operationaid.service.KeyWordUsageRecordService;
import org.springframework.beans.BeanUtils;
import org.springframework.stereotype.Service;

import javax.annotation.Resource;
import java.time.LocalDateTime;
import java.time.LocalTime;
import java.util.List;
import java.util.Optional;
import java.util.stream.Collectors;

/**
 * @author 谢骏
 * @description 针对表【operation_aid_key_word(运营助手-关键词数据)】的数据库操作Service实现
 * @createDate 2023-05-26 16:49:26
 */
@Service
public class KeyWordServiceImpl extends ServiceImpl<KeyWordMapper, KeyWord>
        implements KeyWordService {

    @Resource
    private UserUtil userUtil;


    @Resource
    private KeyWordUsageRecordService keyWordUsageRecordService;

    @Override
    public List<KeyWordTableRespVO> getTable(KeyWordTableReqVO reqVO) {
        LambdaQueryWrapperX<KeyWord> queryWrapper = new LambdaQueryWrapperX<>();
        queryWrapper.eq(StrUtil.isNotBlank(reqVO.getCycle()), KeyWord::getCycle, reqVO.getCycle());
        if (ObjectUtil.isNotNull(reqVO.getCreatedTime())) {
            LocalDateTime start = LocalDateTimeUtil.parse(reqVO.getCreatedTime() + "T00:00:00").with(LocalTime.MIN);
            LocalDateTime end = start.plusDays(1).minusNanos(1);
            queryWrapper.between(KeyWord::getCreateTime, start, end);
        }
        if((reqVO.getSearchPeopleNum()) != null){
            queryWrapper.ge(KeyWord::getSearchPeopleNum, reqVO.getSearchPeopleNum());
        }
        queryWrapper.eq(StrUtil.isNotEmpty(reqVO.getCategoryName()), KeyWord::getCategoryName, reqVO.getCategoryName());
        List<KeyWord> list = baseMapper.selectList(queryWrapper);
        List<KeyWordTableRespVO> keyWordTableRespVOS = list.stream().map(item -> {
                    KeyWordTableRespVO target = new KeyWordTableRespVO();
                    BeanUtils.copyProperties(item, target);
                    target.setCreatedTime(LocalDateTimeUtil.formatNormal(item.getCreateTime().toLocalDate()));
                    return target;
                }
        ).collect(Collectors.toList());
        appendKeyWordTableUsageTab(keyWordTableRespVOS);
        return keyWordTableRespVOS;
    }

    @Override
    public List<String> getCycleList(String categoryName) {
        return baseMapper.selectCycleList(categoryName);
    }

    @Override
    public List<String> getCreateTimeList() {
        return baseMapper.selectCreateTimeList();
    }

    @Override
    public List<String> getCategoryNameList(String cycle) {
        return baseMapper.selectCategoryNameList(cycle);
    }

    /**
     * 对结果列表追加关键词用法标签(选项)
     */
    private void appendKeyWordTableUsageTab(List<KeyWordTableRespVO> respS) {
        Long adId = userUtil.getId();
        if (CollUtil.isEmpty(respS)) {
            return;
        }
        List<KeyWordUsageRecord> records = keyWordUsageRecordService.getListNoGroup(respS.get(0).getCategoryName(), adId);
        respS.forEach(item -> {
            Optional<KeyWordUsageRecord> first = records.stream()
                    .filter(i -> StrUtil.equals(item.getName(), i.getName()))
                    .findFirst();
            if (first.isPresent()) {
                KeyWordUsageRecord usageRecord = first.get();
                item.setIsAppendZtc(usageRecord.getIsAppendZtc() == 1)
                        .setIsGenerateOrderPlan(usageRecord.getIsGenerateOrderPlan() == 1)
                        .setIsGenerateTitle(usageRecord.getIsGenerateTitle() == 1);
            } else {
                item.setIsAppendZtc(false).setIsGenerateOrderPlan(false).setIsGenerateTitle(false);
            }
        });
    }
}